var util = {};


//唤醒主屏幕
util.wakeUp = function(){
    if(!device.isScreenOn()){
        device.wakeUpIfNeeded();
    }
}

//打开APP
util.launch = function(appName) {
    //打开应用
    if (app.launchApp(appName)) {
        sleep(1500)
        toast("启动"+appName);
    } else {
        if(text(appName).findOnce()){
            text(appName).click()
            log("qd1")
        }else{
        toastLog("没有找到" + appName + ",进入下一个应用");
        exit();
        }
        
    }
    var loop = 0;
    while(loop < 5){
        loop++;
        sleep(1000);
        util.UITextClick("允许");
    }

    //设置屏幕缩放
    sleep(3500);
};

//通过坐标点击
util.boundsClick = function(item) {
    var bounds = item.bounds();
    log(bounds)
    if(bounds.centerX()>=0 && bounds.centerY()<=device.height ){
    click(bounds.centerX(),bounds.centerY());
    }else{
        log(bounds.centerY());
    }
    sleep(1000);
}

util.boundsClickdn = function(item) {
    var bounds = item.bounds();
    if(bounds.centerX()>=0 && bounds.centerY()<=device.height ){
        click(bounds.centerX(),bounds.centerY());
        }else{
            log(bounds.centerY());
        }
    sleep(1000);
}

//通过UI点击
util.UIClick = function(eleId) {
    var uiele = id(eleId).findOnce();
    if(uiele){
        uiele.click();
    }
    sleep(1000);
}
util.idClick = function(eleId) {
    var uiele = id(eleId).findOnce();
    var flag = false;
    if(uiele){
        uiele.click();
        flag = true;
    }
    sleep(1000);
    return flag;
}

//通过UI文本点击
util.UITextClick = function(textContent) {
    var uiele = text(textContent).findOnce();
    if(uiele){
        uiele.click();
    }
    sleep(1000);
}
util.textClick = function(textContent) {
    var uiele = text(textContent).findOnce();
    var flag = false;
    if(uiele){
        uiele.click();
        flag = true;
    }
    sleep(1000);
    return flag;
}

//通过UI文本的坐标点击
util.UITextBoundsClick = function(textContent) {
    var thisEle = text(textContent).findOnce();
    var flag = false;
    if (thisEle) {
        util.boundsClick(thisEle);
        flag = true;
    }
    sleep(1000);
    return flag;
}
util.UIIDBoundsClick = function(textContent) {
    var thisEle = id(textContent).findOnce();
    var flag = false;
    if (thisEle) {
        util.boundsClick(thisEle);
        flag = true;
    }
    sleep(1000);
    return flag;
}
util.textBoundsClick = function(textContent) {
    var thisEle = text(textContent).findOnce();
    var flag = false;
    if (thisEle) {
        util.boundsClick(thisEle);
        flag = true;
    }
    sleep(1000);
    return flag;
}

//通过UI点击
util.backToIndex = function(indexFlagText,appName) {
    var indexBtn = false;
    var loop = 0;
    while(!indexBtn){
        

        log("xzzy3");
        sleep(1000);
        indexBtn = text(indexFlagText).findOnce();
        if(!indexBtn){
            back();
            util.UIIDBoundsClick("tt_video_ad_close");
            util.UITextClick("关闭广告");
            sleep(1000);
        }
        //超出退出时长的，做一些特殊处理
        
        if(loop >3 ){
            //无限返回的页面
            var isSucc = util.textClick("关闭");
            if(!isSucc){
                util.textBoundsClick("关闭");
            }
            if(!isSucc){
                util.textBoundsClick("继续阅读");
            }
            //系统的安装页面
            if(!isSucc){
                util.UITextClick("取消");
            }

            if(!isSucc){
                util.UITextClick("确定");
                util.UITextClick("关闭广告");
                util.UIIDBoundsClick("tt_video_ad_close");
                
            }

            if(!isSucc){
                util.UIIDBoundsClick("iv_cancel");
                if(loop>6){
                    if(text("立即下载").findOnce()){
                        click(10,10);
                    }
                }
            }
            if(loop>35){
                util.clears()
                util.launch(appName)
            }
            //成功关闭
            if(isSucc){
                indexBtn = true;
            }
            toastLog("弹窗")
        }
        loop++;
        sleep(1000);
        if(loop>60){
            exit();
        }
    }
}

//滑动阅读新闻
util.swapeToRead = function() {
    //滑动阅读新闻
    swipe(device.width / 2, device.height * 0.7 ,
        device.width / 2, device.height * 0.4, 1500);

    swipe(device.width / 2, device.height * 0.7 ,
        device.width / 2, device.height * 0.4, 1500);
    sleep(1000);
}

util.swapeToReads = function() {
    //滑动阅读新闻
    var storage = storages.create("init");
    var x1=1500;
    var x2=2800;
    if(storage.contains("快速阅读")){
        x1=500;
        x2=1200;
    }  
    util.swipeEx(device.width / 3, device.height * 0.7 ,
        device.width / 2, device.height * 0.3, random(x1, x2)); //向上滑动翻页
        sleep(x1);
}
util.swapeToReadsu = function() {
    //滑动阅读新闻
    util.swipeEx(device.width / 3, device.height * 0.5 ,
        device.width / 2, device.height * 0.7, random(500, 1000)); //向上滑动翻页
}

util.swipeEx =function(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    };

    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    };
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    };
    var dx3 = {
        "x": zx,
        "y": zy
    };
    for (var i = 0; i < 4; i++) {

        eval("point.push(dx" + i + ")");

    };
    // log(point[3].x)

    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(util.bezier_curves(point, i).x), parseInt(util.bezier_curves(point, i).y)]

        xxy.push(xxyy);

    }

    // log(xxy);
    gesture.apply(null, xxy);
};

util.bezier_curves =function(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = {
        "x": 0,
        "y": 0
    };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
};

util.clears = function(){
    //清理缓存
    home();
    sleep(1500);
    recents();
    sleep(1500);
    util.UIIDBoundsClick("clearAnimView");
    util.UIIDBoundsClick("recent_igmbutton_clear_all");
    util.UIIDBoundsClick("clear_recents");
    var gb = desc("全部清除").findOnce()
    if(gb){
        click(gb.bounds().centerX(),gb.bounds().centerY());
        log("关闭")
        sleep(1500);
    }
    toastLog("清理后台应用");
    sleep(1000);
}


//通过UI点击
util.backToVedioIndex = function(indexFlagTexts) {
    var indexBtn = false; 
    var indexBtns = false; 
    var loop = 0;
    while(!indexBtn && !indexBtns){
        
        sleep(2000);
        indexBtn = id(indexFlagTexts).findOnce();
        indexBtns = text(indexFlagTexts).findOnce();
        if(indexBtn==null || indexBtns==null){
        //超出退出时长的，做一些特殊处理
        sleep(1000);
        back();
        if(loop > 5){
            //无限返回的页面
            var isSucc = util.textClick("关闭");
            if(!isSucc){
                util.textBoundsClick("关闭");
            }

            //系统的安装页面
            if(!isSucc){
                util.UITextClick("取消");
            }

            if(!isSucc){
                util.UIIDBoundsClick("iv_cancel");
            }

            //成功关闭
            if(isSucc){
                indexBtn = true;
            }
            toastLog("弹窗")
        }
        loop++;
        if(loop>3){
            exit();
        }
        }
    }
}

module.exports = util;